package com.baitengsoft.patentapimonitor.api.service.impl;

import com.baitengsoft.patentapimonitor.api.dao.PacApplicantDao;
import com.baitengsoft.patentapimonitor.api.dao.PacIndustryDao;
import com.baitengsoft.patentapimonitor.api.dao.RptApplicantDao;
import com.baitengsoft.patentapimonitor.api.dao.WebPlatFormAreaDao;
import com.baitengsoft.patentapimonitor.api.entity.PacIndustryEntity;
import com.baitengsoft.patentapimonitor.api.entity.PacPatentinfoEntity;
import com.baitengsoft.patentapimonitor.api.entity.RptApplicantEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebPlatformareaEntity;
import com.baitengsoft.patentapimonitor.api.model.*;
import com.baitengsoft.patentapimonitor.api.model.param.SearchApplicantAnalysisParam;
import com.baitengsoft.patentapimonitor.api.service.PacIndustryService;
import com.baitengsoft.patentapimonitor.api.service.RptApplicantService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.SearchParam;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权力人报表服务实现类
 *
 * @author Chen 2018/1/5
 */
@Service
public class RptApplicantServiceImpl extends BaseServiceImpl<RptApplicantEntity> implements RptApplicantService {
    @Autowired
    private WebPlatFormAreaDao webPlatFormAreaDao;
    @Autowired
    private PacIndustryDao pacIndustryDao;
    @Autowired
    private PacApplicantDao pacApplicantDao;
    @Autowired
    private RptApplicantDao rptApplicantDao;
    /**
     * 列表
     *
     * @param platformid
     * @return
     */
    public PageResult<WebApplicantInfo> getApplicantAnalysisList(Integer platformid, SearchApplicantAnalysisParam param) {
        Page<Object> page = PageHelper.startPage(param.getPageNumber(), param.getPageSize());
        List<WebApplicantInfo> infos = pacApplicantDao.getApplicantRptList(platformid,param);
        for(WebApplicantInfo info :infos){
            if (StringUtils.isBlank(info.getIndustry()))
                info.setIndustry("未分类");
            if (StringUtils.isBlank(info.getAreaName()))
                info.setAreaName("未分类");
        }
        Long total = page.getTotal();
        PageResult<WebApplicantInfo> result = new PageResult<>(infos,param.getPageNumber(),param.getPageSize(),total.intValue());
        return result;
    }

    @Autowired
    private PacIndustryService pacIndustryService;

    /**
     * 导出权利人列表
     */
    public List<WebApplicantInfo> getApplicantAnalysisAll(Integer platformid) {
        SearchApplicantAnalysisParam param = new SearchApplicantAnalysisParam();
        return pacApplicantDao.getApplicantRptList(platformid,param);
    }
    /**
     * 获取搜索参数 区域名称和行业
     */
    @Override
    public Res getApplicantSearchParam(Integer platformid) {
        //获取平台下所属区域id name
        List<WebPlatformareaEntity> entities = webPlatFormAreaDao.selectAreaByPlatformId(platformid);
        List<WebAreaInfo> webAreaInfos = new ArrayList<>();
        for (WebPlatformareaEntity entity : entities) {
            WebAreaInfo info = new WebAreaInfo();
            info.setAreaId(entity.getId());
            info.setAreaName(entity.getName());
            webAreaInfos.add(info);
        }
        Example example = new Example(PacIndustryEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("platformid", platformid);
        criteria.andEqualTo("status", 1);
        example.orderBy("sort").asc();
        List<PacIndustryEntity> pacIndustryEntities = pacIndustryDao.selectByExample(example);
        List<WebIndustryInfo> webIndustryInfos = new ArrayList<>();
        for (PacIndustryEntity entity : pacIndustryEntities) {
            WebIndustryInfo info = new WebIndustryInfo();
            info.setIndustryId(entity.getId());
            info.setIndustryName(entity.getName());
            webIndustryInfos.add(info);
        }
        return Res.ok().put("areas", webAreaInfos).put("industry", webIndustryInfos);
    }

    /**
     * 获取数据范围：1当前有效量，2专利总量，3今年公开量，4今年授权量
     */
    @Override
    public List<RptApplicantDataTypeInfo> getApplicantDataType() {
        List<RptApplicantDataTypeInfo> infos = new ArrayList<>();
        RptApplicantDataTypeInfo info = new RptApplicantDataTypeInfo();
        info.setDataTypeName("当前有效量");
        info.setDataTypeValue(1);
        infos.add(info);
        info = new RptApplicantDataTypeInfo();
        info.setDataTypeName("专利总量");
        info.setDataTypeValue(2);
        infos.add(info);
        info = new RptApplicantDataTypeInfo();
        info.setDataTypeName("今年公开量");
        info.setDataTypeValue(3);
        infos.add(info);
        info = new RptApplicantDataTypeInfo();
        info.setDataTypeName("今年授权量");
        info.setDataTypeValue(4);
        infos.add(info);
        return infos;
    }

    @Override
    public List<WebApplicantIndustryInfo> getApplicantIndustry(Integer platformid,Integer dataTypeValue) {
        //查出所有行业
        Map<Integer, String> map = pacIndustryService.getIndustryMapByPlatformId(platformid);
        List<WebApplicantIndustryInfo> infos = pacApplicantDao.getApplicantIndustry(platformid,dataTypeValue);
        for(WebApplicantIndustryInfo info : infos){
            if(info.getIndustryId() == null){
                info.setIndustryName("未分类");
                continue;
            }
            String industryName = map.get(info.getIndustryId());
            if (StringUtils.isNotBlank(industryName)){
                info.setIndustryName(industryName);
            }
        }
        return infos;
    }

    /**
     * 权力人类型分布
     */
    @Override
    public List<WebApplicantTypeInfo> getApplicantType(Integer platformid,Integer dataTypeValue) {
//        List<WebApplicantTypeInfo> infoList = new ArrayList<>();
//        WebApplicantTypeInfo totalInfo = getTotalInfo(platformid, dataTypeValue);
//        infoList.add(totalInfo);
        //获取该平台所有的分类
        //List<WebIndustryInfo> industryInfos = pacIndustryService.getIndustryInfoByPlatformId(platformid);
        List<WebApplicantTypeInfo> list = pacApplicantDao.getApplicantTypeList_1(platformid,dataTypeValue);
        WebApplicantTypeInfo total = list.get(0);
        total.setPatPercent("100");
        total.setApplicantPercent("100");
        DecimalFormat fnum = new DecimalFormat("##0.00");
        for (int i = 1; i < list.size(); i++) {
            float a = list.get(i).getApplicantTotal().floatValue() / total.getApplicantTotal().floatValue() * 100;
            list.get(i).setApplicantPercent(fnum.format(a));
            float b = list.get(i).getPatTotal().floatValue() / total.getPatTotal().floatValue() * 100;
            list.get(i).setPatPercent(fnum.format(b));
        }
//        for(WebIndustryInfo info : industryInfos){
//            WebApplicantTypeInfo typeInfo = new WebApplicantTypeInfo();
//            typeInfo.setTypeName(info.getIndustryName());
//            WebApplicantTypeInfo temp = pacApplicantDao.getApplicantTypeList(platformid, dataTypeValue, info.getIndustryId());
//            if(temp!=null){
//                typeInfo.setPatTotal(temp.getPatTotal()== null? new BigDecimal(0):temp.getPatTotal());
//                typeInfo.setApplicantTotal(temp.getApplicantTotal() == null ? new BigDecimal(0):temp.getApplicantTotal());
//                if (totalInfo.getPatTotal().compareTo(new BigDecimal(0))==1){
//                    typeInfo.setPatPercent(temp.getPatTotal().divide(totalInfo.getPatTotal(), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
//                }
//                if (totalInfo.getApplicantTotal().compareTo(new BigDecimal(0))==1){
//                    typeInfo.setApplicantPercent(temp.getApplicantTotal().divide(totalInfo.getApplicantTotal(), 2, RoundingMode.HALF_UP).multiply(new BigDecimal(100)));
//                }
//            }else {
//                typeInfo.setPatTotal(new BigDecimal(0));
//                typeInfo.setApplicantTotal(new BigDecimal(0));
//                typeInfo.setPatPercent(new BigDecimal(0));
//                typeInfo.setApplicantPercent(new BigDecimal(0));
//            }
//            infos.add(typeInfo);
//        }
//        infoList.addAll(infos);
        return list;
    }

    @Override
    public PageResult<WebApplicantInfo> getRank(Integer platformid, Integer type) {
        List<WebApplicantInfo> infos=  rptApplicantDao.getRank(platformid,type);
        return new PageResult<>(infos,1,10,10);
    }

    /**
     * 权力人类型分布 总数信息
     */
    private WebApplicantTypeInfo getTotalInfo(Integer platformid,Integer dataTypeValue){

        WebApplicantTypeInfo total = pacApplicantDao.getTotalInfoByType(platformid,dataTypeValue);
        total.setTypeName("合计");
        total.setApplicantPercent("100%");
        total.setPatPercent("100%");
        return total;
    }

}
